/*
 *  jaspion.cpp
 *  complexidade
 *  http://br.spoj.pl/problems/JASPION/
 *  Created by Bruno Dias on 15/05/11.
 *  Copyright 2011 __MyCompanyName__. All rights reserved.
 *
 */

import java.util.HashMap;
import java.util.Scanner;
 
class Main {
    public static void main(String[] args) {
 
        Scanner s = new Scanner(System.in);
 
        int t = s.nextInt();
        HashMap<String, String> Dictionary = new HashMap<String, String>();
 
        for (int i = 0; i < t; i++) {
                        int m = s.nextInt();
                        int n = s.nextInt();
                s.nextLine();
 
            for (int j = 0; j < m; j++) {
                String word = s.nextLine();
                String trans = s.nextLine();
                Dictionary.put(word, trans);
            }
 
            for (int j = 0; j < n; j++) {
                String text = s.nextLine();
                String[] a = text.split(" ");
 
                for (int l = 0; l < a.length; l++) {
                    if (Dictionary.containsKey(a[l])) {
                        System.out.print(Dictionary.get(a[l]) + " ");
                    } else
                        System.out.print((a[l]) + " ");
                }
                System.out.println("");
            }
            Dictionary.clear();
        }
    }
}